<?php
class Manufacturer extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->check_isvalidated();
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	}
	public function index()
	{
		$data = get_initial_data($this);
		$data['title'] = 'new manufacturer';
		$this->load->view('templates/header', $data);
		$this->load->view('manufacturer/add', $data);
		$this->load->view('templates/footer', $data);
	}
	public function add()
	{
		$data = get_initial_data($this);
		$this->form_validation->set_rules('name', 'name',  'trim|required|xss_clean|callback_check_duplicate');
		if ($this->form_validation->run()){
			$this->load->model('manufacturer_model');
			$this->manufacturer_model->add($this->input->post('name'), 1);
			$data['message'] = Messages::success_create_manufacturer;
			$data['title'] = 'new manufacturer';
			$this->load->view('templates/header',$data);
			$this->load->view('message',$data);
			$this->load->view('templates/footer',$data);
		}else
		{
			$data['title'] = 'new manufacturer';
			$this->load->view('templates/header', $data);
			$this->load->view('manufacturer/add', $data);
			$this->load->view('templates/footer', $data);
		}
	}

	public function  check_duplicate($str){
		$this->load->model('manufacturer_model');
		if ($this->manufacturer_model->get_id($str) == -1){
			return true;
		}
		$this->form_validation->set_message('check_duplicate',  Messages::duplicate_manufacturer);
		return false;
	}

	public function updateManufacturer(){
		if ($this->input->post('manufacturer_id') == null){
			redirect('manufacturer/update');
		}
		$data = get_initial_data($this);
		$this->form_validation->set_rules('name', 'name',  'trim|required|xss_clean|callback_check_duplicate');
		$id = $this->input->post('manufacturer_id');
		if ($this->form_validation->run()){
			$this->load->model('manufacturer_model');
			if( $this->manufacturer_model->update($id,$this->input->post('name'))){
				$data['title'] = 'Update manufacturer';
				$data['message'] = Messages::success_update_manufacturer;
				$this->load->view('templates/header',$data);
				$this->load->view('message',$data);
				$this->load->view('templates/footer',$data);
			}else{

				$data['message'] = Messages::fail_update_manufacturer;
				$this->load->view('templates/header',$data);
				$this->load->view('message',$data);
				$this->load->view('templates/footer',$data);
			}
		}else
		{
			$this->load->model('manufacturer_model');
			$result = $this->manufacturer_model->get_manufacturer_by_id($id);
			$data['result'] = $result;
			$data['title'] = 'new manufacturer';
			$this->load->view('templates/header', $data);
			$this->load->view('manufacturer/update', $data);
			$this->load->view('templates/footer', $data);
		}
	}

	public function update($id)
	{
		$data = get_initial_data($this);
		if ($id != null){
			$data['title']='Update manufacturer';
			$this->load->model('manufacturer_model');
			$result = $this->manufacturer_model->get_manufacturer_by_id($id);
			if ($result != null){
				$data['result'] = $result;
				$this->load->view('templates/header',$data);
				$this->load->view('manufacturer/update',$data);
				$this->load->view('templates/footer',$data);
				return;
			}
		}
		$data['message'] = Messages::unable_find_manufacturer;
		$this->load->view('templates/header',$data);
		$this->load->view('message',$data);
		$this->load->view('templates/footer',$data);
	}

	public function search(){
		$data = get_initial_data($this);
		$this->load->model('manufacturer_model');
		$per_page = 30;
		$search = $this->input->get('search');
		$page = $this->input->get('per_page');
		$data['search'] = $search;
		$config = array();
		$config["base_url"] = site_url() . '/manufacturer/search?search='.$search;
		$config["total_rows"] = $this->manufacturer_model->count_search($search);
		$config["per_page"] = $per_page;
		$config["uri_segment"] = 3;
		$config['page_query_string'] = TRUE;
		$config['use_page_numbers'] = TRUE;
		$this->pagination->initialize($config);
		
		$data["links"] = $this->pagination->create_links();
		
		$data['result'] = $this->manufacturer_model->search($search, $per_page, $page);

		$data['title'] = 'Manufacturer';
		$this->load->view('templates/header', $data);
		$this->load->view('manufacturer/search', $data);
		$this->load->view('templates/footer', $data);
	}

	private function check_isvalidated(){
		if(!$this->session->userdata('validated')){
			redirect('');
		}
	}
}